﻿using System;
using System.Linq;
using IInject.Properties;

namespace IInject.Registration.Scanning
{
	public class InterfaceScanningRule : PublicClassScanningRule
	{
		public InterfaceScanningRule(Type interfaceType)
		{
			if (interfaceType == null)
			{
				throw new ArgumentNullException("interfaceType");
			}

			if (!interfaceType.IsInterface)
			{
				throw new ArgumentException(string.Format(Resources.TypeMustBeAnInterfaceExceptionMessage, interfaceType.FullName));
			}

			InterfaceType = interfaceType;
		}

		public override ScanningRuleType Type
		{
			get
			{
				return ScanningRuleType.Implements;
			}
		}

		public Type InterfaceType { get; private set; }

		public override MatchingResult IsMatch(Type type)
		{
			return new MatchingResult
			{
				From = InterfaceType,
				To = type,
				IsMatch = IsPublicClass(type) && InterfaceType.IsAssignableFrom(type)
			};
		}
	}
}
